import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  groupList: {},
  currentGroup: {},
  groupAdmins: null,
  groupUsers: null,
  filterAdminGroups: null,
  searchGroupCondition: {
    pageSize: 10,
    name: ""
  }
};

const getters = {
  groupList: state => state.groupList,
  currentGroup: state => state.currentGroup,
  searchGroupCondition: state => state.searchGroupCondition,
  groupAdmins: state => state.groupAdmins,
  groupUsers: state => state.groupUsers,
  filterAdminGroups: state => state.filterAdminGroups
};

const actions = {
  getGroupPageS({ commit }, param) {
    return Api.getGroupPage(param).then(ret => {
      commit(types.UPDATE_GROUP_LIST, ret.data.data);
      return ret;
    });
  },
  getAllAdminUserS({ commit }, param) {
    return Api.getAllAdminUser(param).then(ret => {
      commit(types.UPDATE_GROUP_ADMIN_LIST, ret.data.data);
      commit(types.UPDATE_FILTER_GROUP_ADMIN_LIST, ret.data.data);
      return ret;
    });
  },
  getAllGroupUserS({ commit }, param) {
    return Api.getAllGroupUser(param).then(ret => {
      commit(types.UPDATE_GROUP_USER_LIST, ret.data.data);
      return ret;
    });
  },
  getGroupS({ commit }, id) {
    return Api.getGroup({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_GROUP, ret.data.data);
      return ret;
    });
  },
  addOrUpdateGroupS({ commit }, payload) {
    return Api.addOrUpdateGroup({ body: payload }).then(ret => {
      return ret;
    });
  },
  addGroupUserS({ commit }, payload) {
    return Api.addGroupUser({ body: payload }).then(ret => {
      return ret;
    });
  },
  delGroupUserS({ commit }, payload) {
    return Api.delGroupUser({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteGroupS({ commit }, param) {
    return Api.deleteGroup(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_GROUP](state, payload) {
    state.currentGroup = {
      headimgurl: "",
      name: ""
    };
  },
  [types.UPDATE_CURRENT_GROUP](state, payload) {
    console.log(payload);
    state.currentGroup = payload;
  },
  [types.UPDATE_GROUP_LIST](state, payload) {
    state.groupList = payload;
  },
  [types.UPDATE_GROUP_ADMIN_LIST](state, payload) {
    state.groupAdmins = payload;
  },
  [types.UPDATE_FILTER_GROUP_ADMIN_LIST](state, payload) {
    state.filterAdminGroups = payload;
  },
  [types.UPDATE_GROUP_USER_LIST](state, payload) {
    state.groupUsers = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
